﻿#region

using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataInfo;
using KenLib.BusinessManager;

#endregion

public partial class Org_frmO_EmpList : Page
{
    private ErrorManager errManager;
    private Error errObject;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = "団体職員情報一覧";
        ((Org_MasterPage) Master).changeTitle("団体職員情報一覧");
        ((Org_MasterPage) Master).HideLiArea("frmO_EmpList");
        grv_human.PageSize = AppCode_Common.getPageSize();
        // AppCode_Common.ShowEmtyGrid<年数回の賞与>(grv_human, 5, "データーがあります。", "");
        Session.Remove("or00110f_link");
        errManager = new ErrorManager();

        if (Session["役"] == null || (Convert.ToInt32(Session["役"]) != 1 && Convert.ToInt32(Session["役"]) != 2))
        {
            Response.Redirect("frmO_Manager.aspx");
        }
        else
        {
            if (Convert.ToInt32(Session["役"]) == 1)
            {
                ddl_組合.Visible = true;
            }
            else if (Convert.ToInt32(Session["役"]) == 2)
            {
                ddl_組合.Visible = false;
            }
        }

        if (!IsPostBack)
        {
            bind組合();
            CustomValidator1.ErrorMessage = getErr("ER00007", 2);
            if (Session["dtt_frm00055"] == null)
            {
                if (Convert.ToInt32(Session["役"]) == 2)
                {
                    DataTable dtt = searchData(txt_検索.Text.Replace("'", "''"), Session["企業コード_2"].ToString());
                    setDatatoSession(dtt);

                    showGridView();
                }
                else
                {
                    string strHeader = "社員ID,姓名,性別,国籍名,生年月日,携帯番号";
                    ShowGridViewNotClassEmpty(grv_human, strHeader, "データーがありません。");
                }
            }
            else
            {
                showGridView();
                if (Session["txt_frm00055"] != null)
                {
                    txt_検索.Text = Session["txt_frm00055"].ToString();
                }
                if (Session["ddl_frm00055"] != null)
                {
                    ddl_組合.SelectedIndex = Convert.ToInt32(Session["ddl_frm00055"].ToString());
                }
            }
        }
    }

    private String getErr(string errID, int index)
    {
        errObject = new Error();
        errObject.ErrorID = errID;
        DataTable dtErr = errManager.getError(errObject);
        return dtErr.Rows[0][index].ToString();
    }

    private DataTable getDatafromSession()
    {
        DataTable dt = (DataTable) Session["dtt_frm00055"];
        return dt;
    }

    private void setDatatoSession(DataTable dt)
    {
        Session["dtt_frm00055"] = dt;
    }

    /// <summary>
    /// Shows the grid view.
    /// </summary>
    private void showGridView()
    {
        DataTable dt = getDatafromSession();
        if (dt.Rows.Count == 0)
        {
            string strHeader = "社員ID,姓名,性別,国籍名,生年月日,携帯番号";
            ShowGridViewNotClassEmpty(grv_human, strHeader, "データーがありません。");
        }
        else
        {
            grv_human.DataSource = dt.DefaultView;
            grv_human.DataBind();
        }
    }

    protected void grv_human_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_human.PageIndex = e.NewPageIndex;
        showGridView();
    }

    /// <summary>
    /// Get性別s the specified 性別.
    /// </summary>
    /// <param name="性別">The 性別.</param>
    /// <returns></returns>
    protected string get性別(object 性別)
    {
        try
        {
            string result = "";
            if ((bool) 性別)
            {
                result = "女";
            }
            else
            {
                result = "男";
            }

            return result;
        }
        catch
        {
        }
        return null;
    }

    /// <summary>
    /// Searches the data.
    /// </summary>
    /// <param name="search">The search.</param>
    /// <param name="企業ID">The 企業 ID.</param>
    /// <returns></returns>
    private DataTable searchData(string search, string 企業ID)
    {
        frmO_EmpListManager EmplistManager = new frmO_EmpListManager();
        DataTable dt = EmplistManager.searchData(search, 企業ID);

        return dt;
    }

    /// <summary>
    /// Bind組合s this instance.
    /// </summary>
    private void bind組合()
    {
        ddl_組合.Items.Clear();
        frmO_EmpListManager EmplistManager = new frmO_EmpListManager();
        DataTable dt = EmplistManager.select_本社情報();
        if (dt != null && dt.Rows.Count > 0)
        {
            ListItem itm = new ListItem("[選択]", "-1");
            ddl_組合.Items.Add(itm);
            ddl_組合.Items[0].Selected = true;

            foreach (DataRow dtr in dt.Rows)
            {
                ListItem item = new ListItem(dtr["企業名"].ToString(), dtr["企業ID"].ToString());
                ddl_組合.Items.Add(item);
            }
        }
    }

    protected void btn_検索_Click(object sender, EventArgs e)
    {
        Session["txt_frm00055"] = txt_検索.Text;
        Session["ddl_frm00055"] = ddl_組合.SelectedIndex;
        search();
    }

    /// <summary>
    /// Searches this instance.
    /// </summary>
    private void search()
    {
        grv_human.PageIndex = 0;
        if (Convert.ToInt32(Session["役"]) == 1)
        {
            if (ddl_組合.Items.Count < 2)
            {
                setDatatoSession(new DataTable());
                ScriptManager.RegisterStartupScript(upPanel, upPanel.GetType(), "Error",
                                                    "alert('監理団体" + getErr("ER00008", 2) + "')", true);
                showGridView();
            }
            else
            {
                DataTable dtt = searchData(txt_検索.Text.Replace("'", "''"), ddl_組合.SelectedValue);
                setDatatoSession(dtt);
                showGridView();
            }
            ddl_組合.Focus();
        }
        else
        {
            DataTable dtt = searchData(txt_検索.Text.Replace("'", "''"), Session["企業コード_2"].ToString());
            setDatatoSession(dtt);
            showGridView();
            txt_検索.Focus();
        }
        ScriptManager.RegisterStartupScript(upPanel, upPanel.GetType(), "regist", "regist();", true);
    }

    protected void grv_human_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "link")
        {
            String key = grv_human.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();

            frmO_EmpListManager EmplistManager = new frmO_EmpListManager();
            DataTable dt = EmplistManager.check_exist(key);
            if (dt == null || dt.Rows.Count == 0)
            {
                ScriptManager.RegisterStartupScript(upPanel, upPanel.GetType(), "Error",
                                                    "alert('氏名" + getErr("ER00008", 2) + "')", true);
                search();
            }
            else
            {
                Response.Redirect("frmO_EmpBasicInfo.aspx?id=" + key);
            }
        }
        ScriptManager.RegisterStartupScript(upPanel, upPanel.GetType(), "regist", "regist();", true);
    }

    public void ShowGridViewNotClassEmpty(GridView grv, string strTableHeader, string strEmpty)
    {
        DataTable dt = new DataTable();
        string[] arrColumnNames = strTableHeader.Split(',');
        for (int i = 0; i < arrColumnNames.Length; i++)
        {
            dt.Columns.Add(arrColumnNames[i]);
        }
        grv.ShowFooter = false;
        DataRow r = dt.NewRow();
        dt.Rows.InsertAt(r, 0);
        grv.DataSource = dt;
        grv.DataBind();
        grv.Rows[0].Cells.Clear();
        TableCell cell = new TableCell();
        cell.ColumnSpan = grv.Columns.Count;
        cell.Text = strEmpty;
        grv.Rows[0].Cells.Add(cell);
    }
}